<?php
/**
 * 微信支付
 *
 * 实现微信支付，企业付款，网页授权，支付通知功能
 *
 * @package		Hooloo framework
 * @author 		Bill
 * @copyright 	Hooloo Team
 * @version		1.1
 * @release		2017.06.28
 */
if (!defined('BASEPATH')) exit('No direct script access allowed');

//加载配置项
include_once(APPPATH . "config/wx_config.php");

/**
 * 所有接口的基类
 */
class Common_util_pub {
	/**
	 * 	作用：设置参数时需要用到的字符处理函数
	 */
	protected function trimString($value) {
		$ret = null;
		if (null != $value) {
			$ret = $value;
			if (strlen($ret) == 0) {
				$ret = null;
			}
		}
		return $ret;
	}
	
	/**
	 * 	作用：产生随机字符串，不长于32位
	 */
	protected function createNoncestr( $length = 32 ) {
		$chars = "abcdefghijklmnopqrstuvwxyz0123456789";  
		$str = "";
		for ( $i = 0; $i < $length; $i++ )  {  
			$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);  
		}  
		return $str;
	}
	
	/**
	 * 	作用：格式化参数，签名过程需要使用
	 */
	protected function formatBizQueryParaMap($paraMap, $urlencode) {
		$buff = "";
		ksort($paraMap);
		foreach ($paraMap as $k => $v) {
		    if($urlencode) {
			   $v = urlencode($v);
			}
			$buff .= $k . "=" . $v . "&";
		}
		$reqPar;
		if (strlen($buff) > 0) {
			$reqPar = substr($buff, 0, strlen($buff) - 1);
		}
		return $reqPar;
	}
	
	/**
	 * 	作用：生成签名
	 */
	protected function getSign($Obj) {
		foreach ($Obj as $k => $v) {
			$Parameters[$k] = $v;
		}
		//签名步骤一：按字典序排序参数
		ksort($Parameters);
		//签名步骤二：格式化参数
		$String = $this->formatBizQueryParaMap($Parameters, false);
		//签名步骤三：在string后加入KEY
		$String = $String."&key=" . AKEY;
		//签名步骤四：MD5加密
		$String = md5($String);
		//签名步骤五：所有字符转为大写
		$result_ = strtoupper($String);
		//返回签名结果
		return $result_;
	}
	
	/**
	 * 	作用：array转xml
	 */
	protected function arrayToXml($arr) {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
        	if (is_numeric($val)) {
        	 	$xml .= "<" . $key . ">" . $val . "</" . $key . ">"; 
        	} else {
				$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";  
			}
        }
        $xml .= "</xml>";
        return $xml; 
    }
	
	/**
	 * 	作用：将xml转为array
	 */
	protected function xmlToArray($xml) {		
        //将XML转为array        
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);		
		return $array_data;
	}

	/**
	 * 	作用：以post方式提交xml到对应的接口url
	 */
	protected function postXmlCurl($xml, $url, $second = 30) {		
        //初始化curl        
       	$ch = curl_init();
		//设置超时
		curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        //这里设置代理，如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
		//设置header
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		//post提交方式
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
		//运行curl
        $ch_result_ = curl_exec($ch);
		//返回结果
		curl_close($ch);
		return $ch_result_;
	}

	/**
	 * 	作用：使用证书，以post方式提交xml到对应的接口url
	 */
	protected function postXmlSSLCurl($xml, $url, $second = 30) {
		$ch = curl_init();
		//超时时间
		curl_setopt($ch, CURLOPT_TIMEOUT, $second);
		//这里设置代理，如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		//设置header
		curl_setopt($ch, CURLOPT_HEADER,FALSE);
		//要求结果为字符串且输出到屏幕上
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		//设置证书
		//使用证书：cert 与 key 分别属于两个.pem文件
		//默认格式为PEM，可以注释
		curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
		curl_setopt($ch, CURLOPT_SSLCERT, SSLCERT_PATH);
		//默认格式为PEM，可以注释
		curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
		curl_setopt($ch, CURLOPT_SSLKEY, SSLKEY_PATH);
		//post提交方式
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS,$xml);
		$ch_result_ = curl_exec($ch);
		//返回结果
		curl_close($ch);
		return $ch_result_;
	}
}

/**
 * 统一支付接口类
 */
class Unified_pub extends Common_util_pub {
	var $parameters;//请求参数，类型为关联数组
	public $response;//微信返回的响应
	public $result;//返回参数，类型为关联数组
	var $url;//接口链接
	
	public function __construct() {
		//设置接口链接
		$this->url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
	}
	
	/**
	 * 	作用：设置请求参数
	 */
	public function setParameter($parameter, $parameterValue) {
		$this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
	}
	
	/**
	 * 生成接口参数xml
	 */
	protected function createXml() {
		try {
			//检测必填参数
			if(empty($this->parameters["out_trade_no"])) {
				throw new Exception("缺少统一支付接口必填参数out_trade_no！"."<br>"); 
			} elseif (empty($this->parameters["body"])) {
				throw new Exception("缺少统一支付接口必填参数body！"."<br>");
			} elseif (empty($this->parameters["total_fee"])) {
				throw new Exception("缺少统一支付接口必填参数total_fee！"."<br>");
			} elseif (empty($this->parameters["trade_type"])) {
				throw new Exception("缺少统一支付接口必填参数trade_type！"."<br>");
			} elseif ($this->parameters["trade_type"] == "JSAPI" && empty($this->parameters["openid"])){
				throw new Exception("统一支付接口中，缺少必填参数openid！trade_type为JSAPI时，openid为必填参数！"."<br>");
			}
		} catch (Exception $e) {
			exit($e->getMessage());
		}

		//设置全局参数
		$this->parameters["appid"] = APPID;//公众账号ID
		$this->parameters["mch_id"] = MCHID;//商户号
		$this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
		$this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip
		$this->parameters["notify_url"] = NOTIFY_URL;//签名
		if ($this->parameters["trade_type"] == "NATIVE") {
			$this->setParameter("time_start", date("YmdHis"));//交易类型
		}
		$this->parameters["sign"] = $this->getSign($this->parameters);//签名

		//array转xml
		return $this->arrayToXml($this->parameters);
		
	}
	
	/**
	 * 	作用：post请求xml
	 */
	protected function postXml() {
	    $xml = $this->createXml();
		$this->response = $this->postXmlCurl($xml, $this->url, CURL_TIMEOUT);
		return $this->response;
	}
	
	/**
	 * 	作用：使用证书post请求xml
	 */
	protected function postXmlSSL() {	
	    $xml = $this->createXml();
		$this->response = $this->postXmlSSLCurl($xml, $this->url, CURL_TIMEOUT);
		return $this->response;
	}

	/**
	 * 	作用：获取结果，默认不使用证书
	 */
	public function getResult() {
		return $this->result;
	}
	
	/**
	 * 作用：获取prepay_id
	 */
	public function getPrepayId($type = "JSAPI") {
		$this->postXml();
		$this->result = $this->xmlToArray($this->response);
		if ($this->result["return_code"] == "FAIL") {
			return false;
		} else {
			//支付类型：NATIVE-扫码支付，JSAPI-公众号H5支付，MWEB-手机浏览器H5支付
			if ($type == "JSAPI") {
				//公众号H5支付:返回预生成流水号
				$prepay_id = $this->result["prepay_id"];

				$jsApiObj["appId"] = APPID;
				$timeStamp = time();
				$jsApiObj["timeStamp"] = "$timeStamp";
				$jsApiObj["nonceStr"] = $this->createNoncestr();
				$jsApiObj["package"] = "prepay_id=$prepay_id";
				$jsApiObj["signType"] = "MD5";
				$jsApiObj["paySign"] = $this->getSign($jsApiObj);
				return $jsApiObj;
			} elseif ($type == "MWEB") {
				//手机浏览器H5支付:
				return $this->result["prepay_id"];
			} else {
				//扫码支付:返回二维码地址
				return  'http://paysdk.weixin.qq.com/example/qrcode.php?data=' . urlencode($this->result["code_url"]);
			}
		}
	}
}

/**
* JSAPI授权获取个人信息
*/
class Jsapi_pub extends Common_util_pub {
	/**
	 * 	作用：生成可以获得code的url
	 */
	public function createOauthUrlForCode($redirectUrl = '', $scope = 'snsapi_base') {
		if (! $redirectUrl) {
			$redirectUrl = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		}
		$urlObj["appid"] = APPID;
		$urlObj["redirect_uri"] = urlencode("$redirectUrl");
		$urlObj["response_type"] = "code";
		$urlObj["scope"] = $scope;
		$urlObj["state"] = "STATE#wechat_redirect";
		$bizString = $this->formatBizQueryParaMap($urlObj, false);
		return "https://open.weixin.qq.com/connect/oauth2/authorize?" . $bizString;
	}

	/**
	 * 	作用：生成可以获得openid的url
	 */
	public function createOauthUrlForOpenid($code_) {
		$urlObj["appid"] = APPID;
		$urlObj["secret"] = APPSECRET;
		$urlObj["code"] = $code_;
		$urlObj["grant_type"] = "authorization_code";
		$bizString = $this->formatBizQueryParaMap($urlObj, false);
		return "https://api.weixin.qq.com/sns/oauth2/access_token?" . $bizString;
	}
	
	
	/**
	 * 	作用：通过curl向微信提交code，以获取openid
	 */
	public function getOpenid($code_) {
		$url = $this->createOauthUrlForOpenid($code_);
        //初始化curl
       	$ch = curl_init();
		//设置超时
		curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT);
		curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		//运行curl，结果以jason形式返回
        $res = curl_exec($ch);
		curl_close($ch);
		//取出openid
		$data = json_decode($res, true);
		return @$data['openid'];
	}
}

/**
 * 企业付款接口类
 */
class Transfers_pub extends Common_util_pub {
	var $parameters;//请求参数，类型为关联数组
	public $response;//微信返回的响应
	public $result;//返回参数，类型为关联数组
	var $url;//接口链接
	
	public function __construct() {
		//设置接口链接
		$this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
	}
	
	/**
	 * 	作用：设置请求参数
	 */
	public function setParameter($parameter, $parameterValue) {
		$this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
	}
	
	/**
	 * 生成接口参数xml
	 */
	protected function createXml() {
		try {
			//检测必填参数
			if(empty($this->parameters["partner_trade_no"])) {
				throw new Exception("缺少统一支付接口必填参数partner_trade_no！"."<br>"); 
			} elseif (empty($this->parameters["desc"])) {
				throw new Exception("缺少统一支付接口必填参数desc！"."<br>");
			} elseif (empty($this->parameters["amount"])) {
				throw new Exception("缺少统一支付接口必填参数amount！"."<br>");
			} elseif (empty($this->parameters["openid"])){
				throw new Exception("缺少统一支付接口必填参数openid！"."<br>");
			}
		} catch (Exception $e) {
			exit($e->getMessage());
		}

		//设置全局参数
		$this->parameters["mch_appid"] = APPID;//公众账号ID
		$this->parameters["mchid"] = MCHID;//商户号
		$this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
		$this->parameters["check_name"] = "NO_CHECK";//NO_CHECK-不校验真实姓名，FORCE_CHECK-强校验真实姓名
		$this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip
		$this->parameters["sign"] = $this->getSign($this->parameters);//签名

		//array转xml
		return $this->arrayToXml($this->parameters);
	}
	
	/**
	 * 	作用：post请求xml
	 */
	protected function postXml() {
	    $xml = $this->createXml();
		$this->response = $this->postXmlCurl($xml, $this->url, CURL_TIMEOUT);
		return $this->response;
	}
	
	/**
	 * 	作用：使用证书post请求xml
	 */
	protected function postXmlSSL() {	
	    $xml = $this->createXml();
		$this->response = $this->postXmlSSLCurl($xml, $this->url, CURL_TIMEOUT);
		return $this->response;
	}

	/**
	 * 	作用：获取结果，默认不使用证书
	 */
	public function getResult() {
		return $this->result;
	}
	
	/**
	 * 作用：发起付款
	 */
	public function getPrepayId() {
		$this->postXmlSSL();
		$this->result = $this->xmlToArray($this->response);
		return $this->result;
	}
}

/**
 * 响应型接口基类
 */
class Notify_pub extends Common_util_pub {
	public $data;//接收到的数据，类型为关联数组
	var $returnParameters;//返回参数，类型为关联数组
	
	/**
	 * 将微信的请求xml转换成关联数组，以方便数据处理
	 */
	public function saveData($xml) {
		$this->data = $this->xmlToArray($xml);
	}
	
	/**
	 * 校验签名
	 */
	public function checkSign() {
		$tmpData = $this->data;
		unset($tmpData['sign']);
		$sign = $this->getSign($tmpData);//本地签名
		if ($this->data['sign'] == $sign) {
			return TRUE;
		}
		return FALSE;
	}
	
	/**
	 * 获取微信的请求数据
	 */
	public function getData() {		
		return $this->data;
	}
	
	/**
	 * 设置返回微信的xml数据
	 */
	public function setReturnParameter($parameter, $parameterValue) {
		$this->returnParameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
	}
	
	/**
	 * 生成接口参数xml
	 */
	public function createXml() {
		return $this->arrayToXml($this->returnParameters);
	}
	
	/**
	 * 将xml数据返回微信
	 */
	public function returnXml() {
		$returnXml = $this->createXml();
		return $returnXml;
	}
}

/* 
*微信接口封装类 
*/
class Wechat_pub extends Common_util_pub{ 
	private $data = array();
	public function __construct($token = "") {
		if ($token) {
			header('content-type:text');
			ob_clean();
			$this->auth($token) || exit;
			if (IS_GET) { 
				exit($_GET['echostr']);
			} else {
				$xml = file_get_contents("php://input");
				$this->data = $this->xmlToArray($xml);
				$this->data || exit;
			}
		}
	}
	private function auth($token) { 
		$data = array($token, @$_GET['timestamp'], @$_GET['nonce']);
		$signature = @$_GET['signature'];
		sort($data,SORT_STRING);//公众平台开发接口PHP SDK更新，解决有时无法接收用户消息的问题
		$sign = sha1(implode($data));
		if ($sign == $signature) {
			return true;
		} else {
			return false;
		}
	}	
	
	/**
	 * 作用：接收微信服务器数据
	 */
	public function request() {
		return $this->data;
	} 
	
	/**
	 * 作用：响应微信服务器
	 */
	public function response($content, $type = 'text') {
		if (empty($content)) {
			$content = " ";
        }
		$rep = array('ToUserName' => $this->data['FromUserName'], 'FromUserName' => $this->data['ToUserName'], 'CreateTime' => time(), 'MsgType' => $type, 'Content' => $content);
		$xml = $this->arrayToXml($rep);
		exit($xml);
	} 
	
	/**
	 * 作用：获取个人详细信息
	 */
	public function get_access_token() {
		$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . APPID . '&secret=' . APPSECRET;
		$res = $this->postXmlCurl("", $url, CURL_TIMEOUT);
		$res = json_decode($res, true);
		if (isset($res['access_token'])) {
			return $res['access_token'];
		} else {
			return false;
		}
	}
}
?>
